package com.surprise.db.dao;

import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.common.collect.Maps;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import com.surprise.db.entity.BUser;

/**
 * BUserDAO.java
 * @author nick
 * @since 2016年3月14日
 */
@Repository
public class BUserDAO extends SimpleDAO<BUser> {
    public Map<Long, BUser> findMapByMallids(List<Long> mallids, int role) {
        Map<Long, BUser> ret = Maps.newHashMap();
        if (CollectionUtils.isEmpty(mallids)) {
            return ret;
        }
        StringBuilder sb = new StringBuilder();
        String mallIdStr = StringUtils.join(mallids, ',');
        sb.append("select id, name, password, mallid from buser where role = ? and mallid in (").append(mallIdStr).append(") ");
        String sql = sb.toString();
        List<BUser> users = jdbcTemplate.query(sql,new Object[] {
                role
        }, new BeanPropertyRowMapper<BUser>(BUser.class));
        users.forEach(user -> {
            ret.put(user.getMallid(), user);
        });
        return ret;
    }
    
    public Map<Long, BUser> findMapByShopids(List<Long> shopids, int role) {
        Map<Long, BUser> ret = Maps.newHashMap();
        if (CollectionUtils.isEmpty(shopids)) {
            return ret;
        }
        StringBuilder sb = new StringBuilder();
        String shopIdStr = StringUtils.join(shopids, ',');
        sb.append("select id, name, password, shopid from buser where role = ? and shopid in (").append(shopIdStr).append(") ");
        String sql = sb.toString();
        List<BUser> users = jdbcTemplate.query(sql, new Object[]{
                role
        }, new BeanPropertyRowMapper<BUser>(BUser.class));
        users.forEach(user -> {
            ret.put(user.getShopid(), user);
        });
        return ret;
    }
    
    public BUser findByMallid(Long mallid, int role) {
        String sql = "select id, name, password from buser where role = ? and mallid = ?";
        List<BUser> users = jdbcTemplate.query(sql, new Object[] {
                role, mallid
        }, new BeanPropertyRowMapper<BUser>(BUser.class));
        if(CollectionUtils.isNotEmpty(users)) {
            return users.get(0);
        }
        return null;
    }
    
    public BUser findByShopid(Long shopid, int role) {
        String sql = "select id, name, password from buser where role = ? and shopid = ?";
        List<BUser> users = jdbcTemplate.query(sql, new Object[] {
                role, shopid
        }, new BeanPropertyRowMapper<BUser>(BUser.class));
        if(CollectionUtils.isNotEmpty(users)) {
            return users.get(0);
        }
        return null;
    }
    
    public List<BUser> findByName(String name) {
        String sql = "select * from buser where name = ?";
        List<BUser> users = jdbcTemplate.query(sql, new Object[] {
                name
        }, new BeanPropertyRowMapper<BUser>(BUser.class));
        return users;
    }
    
    public int deleteByMallid(Long mallid, int role) {
        String sql = "delete from buser where mallid = ? and role = ?";
        int ret = jdbcTemplate.update(sql, mallid, role);
        return ret;
    }
    
    public int deleteByShopid(Long shopid, int role) {
        String sql = "delete from buser where shopid = ? and role = ?";
        int ret = jdbcTemplate.update(sql, shopid, role);
        return ret;
    }
}
